﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WucAddEmployee : System.Web.UI.UserControl
{
    Adminestrator admin = new Adminestrator();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
        txtID.ReadOnly = false;
        txtID.Text = "";
        txtFN.Text = "";
        txtLN.Text = "";
        
        txtSN.Text = "";
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
         DataTable Container = new DataTable();
         Container = admin.Search(RBLSearch.SelectedValue, txtSearch.Text);
         if (Container.Rows.Count > 0)
        {
           
            grdv.DataSource = Container;
            grdv.DataBind();
            lblMsg.Text = "Select Any Employee To Update OR Remove .. ";

        }
        else
        {
            lblMsg.Text = "No Data For This Value Of Search!";
            grdv.DataSource = Container;
            grdv.DataBind();
            
        }
        grdv.SelectedIndex = -1;
    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
        if (admin.DeleteEmployee(grdv.SelectedRow.Cells[1].Text))
            lblMsg.Text = "Employee Deleted Successfully";
        else
            lblMsg.Text = "Employee not Deleted";
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        txtID.Text = grdv.SelectedRow.Cells[1].Text;
        txtFN.Text = grdv.SelectedRow.Cells[2].Text;
        txtSN.Text = grdv.SelectedRow.Cells[3].Text;
        txtLN.Text = grdv.SelectedRow.Cells[4].Text;
        DDLJob.SelectedValue = grdv.SelectedRow.Cells[5].Text;
        DDlCenter.SelectedValue = grdv.SelectedRow.Cells[6].Text;
        
        MultiView1.ActiveViewIndex = 1;
        txtID.ReadOnly = true;
    }
    protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
    {

    }
    protected void grdv_SelectedIndexChanged(object sender, EventArgs e)
    {
        btnRemove.Enabled = true;
        btnUpdate.Enabled = true;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        admin.Center = DDlCenter.SelectedValue;
        admin.FName = txtFN.Text;
        admin.ID = txtID.Text;
        admin.Job = DDLJob.SelectedValue;
        admin.LName = txtLN.Text;
        admin.SName = txtSN.Text;
        if (txtID.ReadOnly)
        {
            if (admin.UpdateEmployee())
            {
                lblMsg.Text = "Employee Updated Successfully";
                MultiView1.ActiveViewIndex = 0;
            }
            else
                lblMsg.Text = "Employee not Updated";
        }
        else
        {
           
            if (admin.AddEmployee())
            {

                lblMsg.Text = "Employee Added Successfully";
                MultiView1.ActiveViewIndex = 0;
            }
            
 
            else
            {
                
                MultiView1.ActiveViewIndex = 0;
                lblMsg.Text = "Employee not Added , Employee ID must be unique!";
                   
                
            }
               
           
        }
        btnUpdate.Enabled = false;
        btnRemove.Enabled = false;
        grdv.SelectedIndex = -1;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
        grdv.SelectedIndex = -1;
        btnRemove.Enabled = false;
        btnUpdate.Enabled = false;
    }
    
}